#include<iostream>
using namespace std;
#include<vector>
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int k = nums.size()-1;//k用来从数组尾部遍历
        vector<int> result(nums.size(), 0);
        for(int head = 0,tail = nums.size()-1;head<=tail;)
        {
            if(nums[head]*nums[head] < nums[tail]*nums[tail])
            {
                result[k--] = nums[tail]*nums[tail];
                tail--;
                
            }
            else
            {
                result[k--] = nums[head]*nums[head];
                head++;
            }
        }
        return result;
    }
};
int main() {
    Solution solution;
    vector<int> nums = {-4, -1, 0, 3, 10};

    vector<int> result = solution.sortedSquares(nums);

    cout << "输出结果result为: ";
    for (int i = 0; i < result.size(); i++) {
        cout << result[i] << " ";
    }
    cout << endl;

    return 0;
}